from django.shortcuts import *
from django.http import *
from .models import *


# Create your views here.
def index(request):
    return render(request, 'index.html')


def login(request):
    return render(request, 'login.html')


def loginoy(request):
    if request.method == 'POST':
        name = request.POST.get('name')
        pwd = request.POST.get('pwd')
        user = User.objects.values('uname').filter(uname=name, pwd=pwd)
        if user:
            for i in user:
                d = i['uname']
                request.session['name'] = d
            return JsonResponse({'flag':1})
        return JsonResponse({'flag':0})


def all(request):
    all = Info.objects.all()
    return render(request, 'all.html', {'alls': all})


def add(request):
    if request.method == 'GET':
        Ty = Type.objects.all()
        return render(request, 'add.html', {'Ty': Ty})
    else:
        tit = request.POST.get('tit')
        ath = request.POST.get('ath')
        cont = request.POST.get('cont')
        dataa = request.POST.get('dataa')
        bn = request.POST.get('bn')
        type = request.POST.get('type')
        new = Info(tit=tit, ath=ath, cont=cont, dataa=dataa, Type_id=type,bn=bn)
        new.save()
        return redirect(reverse('all'))


def delete(request, id):
    all = Info.objects.filter(pk=id)
    all.delete()
    return redirect(reverse('all'))


def update(request):
    return None
